package com.yc.demo.d0801;

import com.yc.demo.util.Utils;
import com.yc.jdbc.d0605.JdbcTemplate;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "QueryProdcutServlet",
        value = "/queryProduct.s")
public class QueryProdcutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 第几页
        final String page = request.getParameter("page");
        // 每页行数
        final String size = request.getParameter("size");
        // mysql 分页语法   ? 第几行开始查询  (page-1)*size  ? 查几行 => size
        String sql = "select * from product limit ?,?";
        int iPage = Integer.parseInt(page);
        int iSize = Integer.parseInt(size);
        int begin = (iPage - 1) * iSize;
        // 当前页数据
        final List<Map<String, Object>> list = JdbcTemplate.select(sql, begin, iSize);

        sql = "select count(*) cnt from product";
        final List<Map<String, Object>> res = JdbcTemplate.select(sql);
        // 总行数
        final Object total = res.get(0).get("cnt");

        Map<String,Object> result = new HashMap<>();
        result.put("rows", list);
        result.put("total", total);

        Utils.result(response, result);

    }
}
